//加载layui模块
var form = layui.form;
//获取轮播图列表
function getswiper() {
  $.ajax({
    type: "get",
    url: "http://localhost:8888/admin/swipers",
    // headers: {
    //   Authorization:
    //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTQ3ODk0OCwiZXhwIjoxNTk5NDgyNTQ4fQ.yhRtqLW_iaMzxxhNPQfOGnmJWQ0Hd4OkIPHVTZmuOW4",
    // },
    headers: {
      Authorization: localStorage.getItem('token')
    },
    success: function (res) {
      if (res.status === 0) {
        // console.log(res);
        var html = template("table-tpl", res);
        $(".layui-table tbody").html(html);
      }
    },
  });
}
getswiper();
//切换图片状态
$(".layui-table tbody").on("click", ".layui-badge", function (e) {
  //先获取图片的id 和 状态
  var id = $(e.target).data("id");
  var status = $(e.target).data("status");
  $.ajax({
    type: "put",
    url: "http://localhost:8888/admin/swipers/" + id,
    // headers: {
    //   Authorization:
    //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTQ3ODk0OCwiZXhwIjoxNTk5NDgyNTQ4fQ.yhRtqLW_iaMzxxhNPQfOGnmJWQ0Hd4OkIPHVTZmuOW4",
    // },
    headers: {
      Authorization: localStorage.getItem('token')
    },
    data: {
      status: status,
    },
    success: function (res) {
      if (res.status === 0) {
        layer.msg(res.message);
        getswiper();
      }
    },
  });
});

//删除
$(".layui-table tbody").on("click", ".delete", function (res) {
  //获取id
  var id = $(res.target).data("id");
  layer.confirm("确定要删除么？", function (i) {
    $.ajax({
      type: "delete",
      url: "http://localhost:8888/admin/swipers/" + id,
      // headers: {
      //   Authorization:
      //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTQ3ODk0OCwiZXhwIjoxNTk5NDgyNTQ4fQ.yhRtqLW_iaMzxxhNPQfOGnmJWQ0Hd4OkIPHVTZmuOW4",
      // },
      headers: {
        Authorization: localStorage.getItem('token')
      },
      success: function (e) {
        if (e.status === 0) {
          layer.close(i);
          getswiper();
        }
      },
    });
  });
});
//上传文件
$("#uploadSwiper").click(function () {
  $("#myfile").click();
});
//事件监听
$("#myfile").change(function (e) {
  //   console.log(e);
  var files = e.target.files;
  var fd = new FormData();
  $.each(files, function (i, item) {
    fd.append("swipers", item);
  });
  $.ajax({
    type: "POST",
    url: "http://localhost:8888/admin/swipers",
    data: fd,
    // headers: {
    //   Authorization:
    //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTQ3ODk0OCwiZXhwIjoxNTk5NDgyNTQ4fQ.yhRtqLW_iaMzxxhNPQfOGnmJWQ0Hd4OkIPHVTZmuOW4",
    // },
    headers: {
      Authorization: localStorage.getItem('token')
    },
    //表示不需要把fd转成查询字符串
    processData: false,
    //告诉JQuery不要加content-type请求头，否则会报错
    contentType: false,
    success: function (res) {
      if (res.status === 0) {
        layer.msg(res.message);
        getswiper();
      }
    },
  });
});